package com.gzu.emr.controller;

import com.gzu.emr.service.DrugService;
import com.gzu.emr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

import static com.gzu.emr.util.ResultGenerator.genFailResult;
import static com.gzu.emr.util.ResultGenerator.genSuccessResult;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping("/drugs")
public class DrugController {

    @Resource
    private DrugService drugService;

    @ResponseBody
    @RequestMapping(value = "/list", method = GET)
    @Operation(description = "总览药品")
    public Result list() {
        var drugs =  drugService.listDrug();

        return genSuccessResult(drugs);
    }

    @ResponseBody
    @RequestMapping(value = "/list/name", method = POST)
    @Operation(description = "根据药品名查找")
    @Parameters(@Parameter(name = "name", description = "药品名", required = true))
    public Result getByName(@RequestBody Map<String, String> params) {
        var name = params.get("name");
        if(name == null || name.length() == 0) {
            return genFailResult("server internal error");
        }
        var drugs =  drugService.getDrugByName(name);

        return genSuccessResult(drugs);
    }

    @ResponseBody
    @RequestMapping(value = "/list/code", method = POST)
    @Operation(description = "根据药品代码查找")
    @Parameters(@Parameter(name = "name", description = "药品代码", required = true))
    public Result getByCode(@RequestBody Map<String, String> params) {
        var code = params.get("code");
        if(code == null || code.length() == 0) {
            return genFailResult("server internal error");
        }
        var drugs =  drugService.getDrugByCode(code);

        return genSuccessResult(drugs);
    }

    @ResponseBody
    @RequestMapping(value = "/list/generic_name", method = POST)
    @Operation(description = "根据药品通用名称查找")
    @Parameters(@Parameter(name = "generic_ame", description = "药品通用名", required = true))
    public Result getByGenericName(@RequestBody Map<String, String> params) {
        var name = params.get("generic_name");
        if(name == null || name.length() == 0) {
            return genFailResult("server internal error");
        }
        var drugs =  drugService.getDrugByGenericName(name);

        return genSuccessResult(drugs);
    }

}
